//
//  AlarmDisposeDetailCell.m
//  efairy
//
//  Created by 李锋 on 2019/2/27.
//  Copyright © 2019年 East. All rights reserved.
//

#import "AlarmDisposeDetailCell.h"
#import "EFANoticePubCCell.h"
#import "EFAModel.h"
#import <UIImageView+WebCache.h>
#import "DeviceListCell.h"



@interface AlarmDisposeDetailCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIImage *defalutImg;

@end
static NSString *const kReuseCellId = @"EFANoticePubCCell";
@implementation AlarmDisposeDetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initView];
    }
    return self;
}

-(void)initView{
    
//    _collectionViewHeight = [[NSLayoutConstraint alloc]init];
    UIView *headView = [[UIView alloc]init];
    headView.backgroundColor = [UIColor whiteColor];
    headView.layer.cornerRadius = 6;
    headView.layer.masksToBounds = YES;
    
    _headImg = [[UIImageView alloc]init];
    _passageawy = [[UILabel alloc]init];//通道名和通道号
    _passageawy.textColor = ColorFromRGB(0x333333);
    _passageawy.font = [UIFont boldSystemFontOfSize:16.f];
    
    _disposeUser = [[UILabel alloc]init];//处理人员
    _disposeUser.textColor = ColorFromRGB(0x999999);
    _disposeUser.font = [UIFont boldSystemFontOfSize:14.f];
    [headView sd_addSubviews:@[_headImg,_passageawy,_disposeUser]];
    _headImg.sd_layout.widthIs(48).heightIs(48)
    .leftSpaceToView(headView, 12)
    .topSpaceToView(headView, 11);
    
    _passageawy.sd_layout.heightIs(15)
    .leftSpaceToView(_headImg, 12)
    .rightSpaceToView(headView, 12)
    .topSpaceToView(headView, 16);
    
    _disposeUser.sd_layout.heightIs(14)
    .leftSpaceToView(_headImg, 12)
    .rightEqualToView(_passageawy)
    .topSpaceToView(_passageawy, 10);
    
    
    UIView *contentView  = [[UIView alloc]init];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.cornerRadius = 6;
    contentView.layer.masksToBounds = YES;
    
    _alarmTypeTitle = [[UILabel alloc]init];
    _alarmTypeTitle.textColor = ColorFromRGB(0x333333);
    _alarmTypeTitle.font = [UIFont systemFontOfSize:15.f];
    _alarmTypeTitle.text = @"报警类型";
    _alarmType = [[UILabel alloc]init];//报警类型
    _alarmType.textColor = ColorFromRGB(0x999999);
    _alarmType.font = [UIFont systemFontOfSize:15.f];
    _alarmType.textAlignment = NSTextAlignmentRight;
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = ColorFromRGB(0xe5e5e5);
    
    _alarmValueTitle = [[UILabel alloc]init];
    _alarmValueTitle.textColor = ColorFromRGB(0x333333);
    _alarmValueTitle.font = [UIFont systemFontOfSize:15.f];
    _alarmValueTitle.text = @"报警值/阈值";
    _alarmValue = [[UILabel alloc]init];//报警值/阈值
    _alarmValue.textColor = ColorFromRGB(0x999999);
    _alarmValue.font = [UIFont systemFontOfSize:15.f];
    _alarmValue.textAlignment = NSTextAlignmentRight;
    
    UIView *line2 = [[UIView alloc]init];
    line.backgroundColor = ColorFromRGB(0xe5e5e5);
    
    _alarmTimeTitle = [[UILabel alloc]init];
    _alarmTimeTitle.textColor = ColorFromRGB(0x333333);
    _alarmTimeTitle.font = [UIFont systemFontOfSize:15.f];
    _alarmTimeTitle.text = @"报警时间";
    _alarmTime = [[UILabel alloc]init];//报警时间
    _alarmTime.textColor = ColorFromRGB(0x999999);
    _alarmTime.font = [UIFont systemFontOfSize:15.f];
    _alarmTime.textAlignment = NSTextAlignmentRight;
    
    UIView *line3 = [[UIView alloc]init];
    line.backgroundColor = ColorFromRGB(0xe5e5e5);
    
    _disposeTimeTitle = [[UILabel alloc]init];
    _disposeTimeTitle.textColor = ColorFromRGB(0x333333);
    _disposeTimeTitle.font = [UIFont systemFontOfSize:15.f];
    _disposeTimeTitle.text = @"处理时间";
    _disposeTime = [[UILabel alloc]init];//处理时间
    _disposeTime.textColor = ColorFromRGB(0x999999);
    _disposeTime.font = [UIFont systemFontOfSize:15.f];
    _disposeTime.textAlignment = NSTextAlignmentRight;
    
    UIView *line4 = [[UIView alloc]init];
    line4.backgroundColor = ColorFromRGB(0xe5e5e5);
    
    _disposeResultTitle = [[UILabel alloc]init];
    _disposeResultTitle.textColor = ColorFromRGB(0x333333);
    _disposeResultTitle.font = [UIFont systemFontOfSize:15.f];
    _disposeResultTitle.text = @"处理结果";
    _disposeResult = [[UILabel alloc]init];//处理结果
    _disposeResult.textColor = ColorFromRGB(0x999999);
    _disposeResult.font = [UIFont systemFontOfSize:15.f];
    _disposeResult.textAlignment = NSTextAlignmentRight;
    
    _describeContent = [[UILabel alloc]init];//描述内容
    
    _describeImageTitle = [[UILabel alloc]init];//处理图片title
    _describeImageTitle.textColor = ColorFromRGB(0x333333);
    _describeImageTitle.font = [UIFont systemFontOfSize:15.f];
    _describeImageTitle.text = @"处理图片";
    
    [contentView sd_addSubviews:@[_alarmTypeTitle,_alarmType,line,_alarmValueTitle,_alarmValue,line2,_alarmTimeTitle,_alarmTime
                                  ,line3,_disposeTimeTitle,_disposeTime,line4,_disposeResultTitle,_disposeResult]];
    
    _alarmTypeTitle.sd_layout.widthIs(93).heightIs(15).leftSpaceToView(contentView, 12).topSpaceToView(contentView, 15);
    _alarmType.sd_layout.widthIs(200).heightIs(15).rightSpaceToView(contentView, 12).topSpaceToView(contentView, 15);
    line.sd_layout.heightIs(0.5).leftSpaceToView(contentView, 0).rightSpaceToView(contentView, 0).topSpaceToView(_alarmTypeTitle, 15);
    
    _alarmValueTitle.sd_layout.widthIs(93).heightIs(15).leftSpaceToView(contentView, 12).topSpaceToView(line, 15);
    _alarmValue.sd_layout.widthIs(200).heightIs(15).rightSpaceToView(contentView, 12).topSpaceToView(line, 15);
    line2.sd_layout.heightIs(0.5).leftSpaceToView(contentView, 0).rightSpaceToView(contentView, 0).topSpaceToView(_alarmValueTitle, 15.5);;
    
    _alarmTimeTitle.sd_layout.widthIs(93).heightIs(15).leftSpaceToView(contentView, 12).topSpaceToView(line2, 15);
    _alarmTime.sd_layout.widthIs(200).heightIs(15).rightSpaceToView(contentView, 12).topSpaceToView(line2, 15);
    line3.sd_layout.heightIs(0.5).leftSpaceToView(contentView, 0).rightSpaceToView(contentView, 0).topSpaceToView(_alarmTimeTitle, 15.5);;
    
    _disposeTimeTitle.sd_layout.widthIs(93).heightIs(15).leftSpaceToView(contentView, 12).topSpaceToView(line3, 15);
    _disposeTime.sd_layout.widthIs(200).heightIs(15).rightSpaceToView(contentView, 12).topSpaceToView(line3, 15);
    line4.sd_layout.heightIs(0.5).leftSpaceToView(contentView, 0).rightSpaceToView(contentView, 0).topSpaceToView(_disposeTimeTitle, 15.5);;
    
//    _deviceStatusTitle.sd_layout.widthIs(93).heightIs(15)
//    .leftSpaceToView(contentView, 12).topSpaceToView(contentView, 15).topSpaceToView(line4, 15);
//    _deviceStatus.sd_layout.widthIs(200).heightIs(15).rightSpaceToView(contentView, 12).topSpaceToView(line4, 15);
//    line5.sd_layout.heightIs(0.5).leftSpaceToView(contentView, 0).rightSpaceToView(contentView, 0).topSpaceToView(_deviceStatusTitle, 15);;
    
    _disposeResultTitle.sd_layout.widthIs(93).heightIs(15).leftSpaceToView(contentView, 12).topSpaceToView(line4, 15);
    _disposeResult.sd_layout.widthIs(200).heightIs(15).rightSpaceToView(contentView, 12).topSpaceToView(line4, 15);
   
    
    UIView *describeView = [[UIView alloc]init];
    describeView.backgroundColor = [UIColor whiteColor];
    describeView.layer.cornerRadius = 6;
    describeView.layer.masksToBounds = YES;
    _describeContent = [[UILabel alloc]init];
    _describeContent.textColor = ColorFromRGB(0x666666);
    _describeContent.font = [UIFont systemFontOfSize:14.f];
    
    [describeView addSubview:_describeContent];
    
    _describeContent.sd_layout.topSpaceToView(describeView, 16)
    .leftSpaceToView(describeView, 12)
    .rightSpaceToView(describeView, 12)
    .autoHeightRatio(0);
    
    
     _imageTitleView = [[UIView alloc]init];
     _imageTitleView.backgroundColor = [UIColor whiteColor];

    _describeImageTitle = [[UILabel alloc]init];
    _describeImageTitle.text = @"处理图片";
    _describeImageTitle.font = [UIFont systemFontOfSize:15.f];
    _describeImageTitle.textColor = ColorFromRGB(0x333333);
    
    [_imageTitleView addSubview:_describeImageTitle];
    _describeImageTitle.sd_layout.topSpaceToView(_imageTitleView, 15)
    .leftSpaceToView(_imageTitleView, 12)
    .rightSpaceToView(_imageTitleView, 12);
    
    UIView *imageContentView = [[UIView alloc]init];
    imageContentView.backgroundColor = [UIColor whiteColor];
    
    _noImageHint = [[UILabel alloc]init];
    _noImageHint.text = @"没有上传相关图片";
    _noImageHint.font = [UIFont systemFontOfSize:15.f];
    _noImageHint.textColor = ColorFromRGB(0x666666);
    _noImageHint.hidden = YES;
    
    _layout = [[UICollectionViewFlowLayout alloc] init];
    /**
     创建collectionView
     */
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:_layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    /**
     注册item和区头视图、区尾视图
     */
    UINib *cellNib = [UINib nibWithNibName:kReuseCellId bundle:nil];
    [_collectionView registerNib:cellNib forCellWithReuseIdentifier:kReuseCellId];
    
   
    
    [imageContentView sd_addSubviews:@[_noImageHint,_collectionView]];
    
    _noImageHint.sd_layout.heightIs(15).topSpaceToView(imageContentView, 10)
    .leftSpaceToView(imageContentView, 12)
    .rightSpaceToView(imageContentView, 12);
    
    
    _collectionView.sd_layout.topSpaceToView(imageContentView, 10)
    .leftSpaceToView(imageContentView, 12).rightSpaceToView(imageContentView, 12);
    
    
    
     [self.contentView sd_addSubviews:@[headView,contentView,describeView,_imageTitleView,imageContentView]];
    
    headView.sd_layout.heightIs(70).leftSpaceToView(self.contentView, 8)
    .rightSpaceToView(self.contentView, 8)
    .topSpaceToView(self.contentView, 8);
    
    contentView.sd_layout.heightIs(230).leftSpaceToView(self.contentView, 8)
    .rightSpaceToView(self.contentView, 8)
    .topSpaceToView(headView, 8);
    
    describeView.sd_layout.heightIs(100).leftSpaceToView(self.contentView, 8)
    .rightSpaceToView(self.contentView, 8)
    .topSpaceToView(contentView, 8);
    
    _imageTitleView.sd_layout.heightIs(44).leftSpaceToView(self.contentView, 8)
    .rightSpaceToView(self.contentView, 8)
    .topSpaceToView(describeView, 8);
    
    
    imageContentView.sd_layout.
    leftSpaceToView(self.contentView, 8)
    .rightSpaceToView(self.contentView, 8)
    .topSpaceToView(_imageTitleView, 1);
    
    
    self.contentView.backgroundColor = ColorFromRGB(0xeff0f4);
    [self setupAutoHeightWithBottomView:imageContentView bottomMargin:0];
    
 
   
}


- (void)drawRect:(CGRect)rect{
    //左上角 右上角圆角
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: _imageTitleView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(6,6)];
    //创建 layer
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = _imageTitleView.bounds;
    //赋值
    maskLayer.path = maskPath.CGPath;
    _imageTitleView.layer.mask = maskLayer;
    
    //左下角 右下角圆角
    UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect: _collectionView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(6,6)];
    //创建 layer
    CAShapeLayer *maskLayer2 = [[CAShapeLayer alloc] init];
    maskLayer2.frame = _collectionView.bounds;
    //赋值
    maskLayer2.path = maskPath2.CGPath;
    _collectionView.layer.mask = maskLayer2;
}

-(void)setModelDatas:(AlarmDisposeDetailModel *)data{
    
   
    
     NSString *headerImgUrl = data.efairyuser_headimg_url;
    [_headImg sd_setImageWithURL:[NSURL URLWithString:(headerImgUrl ? headerImgUrl : @"")]
                                placeholderImage:[UIImage imageNamed:@"icon_default_head"]];
    _passageawy.text = data.efairydevicealarmstatistics_c_name;
    _disposeUser.text =[NSString stringWithFormat:@"处理人员：%@", data.efairyuser_nickname];
    _alarmType.textColor = ColorFromRGB([DeviceListCell getDeviceStateColor:(int)data.efairydevicealarmstatistics_alarm_type]);
    _alarmType.text = [DeviceListCell getDeviceState:((int)data.efairydevicealarmstatistics_alarm_type)];//报警类型
    _alarmValue.text = data.efairydevicealarmstatistics_highest_value;//报警值/阈值
    _alarmTime.text = data.efairydevicealarmstatistics_start_time;//报警之间
    _disposeTime.text = data.efairyalarmrecord_add_time;//处理时间
    _disposeResult.text = data.efairyalarmrecord_is_handle == 0?@"未解决":@"解决";//处理结果
    _describeContent.text = data.efairyalarmrecord_content;//描述内容
    
    [self configViewWithImgArray:data.efairyalarmrecordimage_list];
}

- (void)configViewWithImgArray:(NSMutableArray *)imgArray {
    if(imgArray && imgArray.count > 0){
        _noImageHint.hidden = YES;
        self.collectionView.hidden = NO;
        self.imgArray = imgArray;
        NSInteger row = imgArray.count / 3 + ((imgArray.count % 3) ? 1 : 0);
        CGFloat cellWidth = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*15 - 2 * 15) / 3.0;
        CGFloat height = cellWidth * row + (row - 1) * 15+34.f;
        if (!row) {
            height = 0;
        }
//        self.collectionViewHeight.constant = height;
        _collectionView.height = height;
        self.collectionView.scrollEnabled = NO;
        [self.collectionView reloadData];
    }else{
        _noImageHint.hidden = NO;
        self.collectionView.hidden = YES;
    }
    
}



#pragma mark - UICollectionViewDataSource
/**
 分区个数
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
/**
 每个分区item的个数
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgArray.count;
}
/**
 创建cell
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    EFANoticePubCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kReuseCellId forIndexPath:indexPath];
    cell.closeBtn.hidden = YES;
    cell.contentImgView.contentMode = UIViewContentModeScaleAspectFill;
    if (indexPath.row < self.imgArray.count) {
        NSString *urlStr = self.imgArray[indexPath.row];
        [cell.contentImgView sd_setImageWithURL:[NSURL URLWithString:urlStr]
                               placeholderImage:self.defalutImg
                                      completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                                          ;
                                      }];
    }
    return cell;
}



/**
 创建区头视图和区尾视图
 */
//-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    if (kind == UICollectionElementKindSectionHeader){
//        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MyCollectionViewHeaderView" forIndexPath:indexPath];
//        headerView.backgroundColor = [UIColor yellowColor];
//        UILabel *titleLabel = [[UILabel alloc]initWithFrame:headerView.bounds];
//        titleLabel.text = [NSString stringWithFormat:@"第%ld个分区的区头",indexPath.section];
//        [headerView addSubview:titleLabel];
//        return headerView;
//    }else if(kind == UICollectionElementKindSectionFooter){
//        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MyCollectionViewFooterView" forIndexPath:indexPath];
//        footerView.backgroundColor = [UIColor blueColor];
//        UILabel *titleLabel = [[UILabel alloc]initWithFrame:footerView.bounds];
//        titleLabel.text = [NSString stringWithFormat:@"第%ld个分区的区尾",indexPath.section];
//        [footerView addSubview:titleLabel];
//        return footerView;
//    }
//    return nil;
//
//
//}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*15 - 2 * 15) / 3.0;
    return CGSizeMake(width, width);
}
/**
 点击某个cell
 */
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(devInfoImgTCell:imgViewTapped:indexPath:)]) {
        NSMutableArray *imgViewMArray = [NSMutableArray arrayWithCapacity:self.imgArray.count];
        for (NSInteger i = 0; i < self.imgArray.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            EFANoticePubCCell *cell = (EFANoticePubCCell *)[collectionView cellForItemAtIndexPath:indexPath];
            if (cell) {
                [imgViewMArray addObject:cell.contentImgView];
            }
        }
        [self.delegate devInfoImgTCell:self imgViewTapped:imgViewMArray indexPath:indexPath];
    }
}

/**
 cell的大小
 */
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    CGFloat itemW = (ScreenWidth-(kLineNum+1)*kLineSpacing)/kLineNum-0.001;
//    return CGSizeMake(itemW, itemW);
//}
/**
 每个分区的内边距（上左下右）
 */
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//    return UIEdgeInsetsMake(kLineSpacing, kLineSpacing, kLineSpacing, kLineSpacing);
//}
/**
 分区内cell之间的最小行间距
 */
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//    return kLineSpacing;
//}
/**
 分区内cell之间的最小列间距
 */
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//    return kLineSpacing;
//}
/**
 区头大小
 */
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    return CGSizeMake(kScreenWidth, 65);
//}
/**
 区尾大小
 */
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
//    return CGSizeMake(kScreenWidth, 65);
//}

@end
